package com.weme.common.util;




import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.mobile.client.AWSMobileClient;
import com.amazonaws.mobile.client.Callback;
import com.amazonaws.mobile.client.UserStateDetails;
import com.amazonaws.mobile.config.AWSConfiguration;
import com.amazonaws.mobileconnectors.s3.transferutility.TransferListener;
import com.amazonaws.mobileconnectors.s3.transferutility.TransferObserver;
import com.amazonaws.mobileconnectors.s3.transferutility.TransferUtility;
import com.amazonaws.regions.Region;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.weme.common.BaseApplication;

import org.json.JSONException;

import java.io.File;
import java.util.concurrent.CountDownLatch;

/**
 * 文件上传工具类
 */
public class AwsS3UploadUtil {

    public static final String BUCKET_NAME = "chatchat"; // 存储图片桶

    public static final String APP_LOG_PATH = "web_log"; // 存放App日志
    private static AmazonS3Client sS3ClientOld;
    private static TransferUtility sTransferUtility;
    private static AWSCredentialsProvider sMobileClient;

    public static TransferObserver upload(String fileName, File file) {
        return upload(getImageFolder(), fileName, file);
    }

    //配置bucket下的文件夹路径
    public static String getImageFolder() {

        return BUCKET_NAME  +"/"+UserMKHelper.INSTANCE.decodeLong(UserMKHelper.INSTANCE.getUID());
    }

    public static String getVideoFolder() {

        return BUCKET_NAME  +"/"+UserMKHelper.INSTANCE.decodeLong(UserMKHelper.INSTANCE.getUID());
    }

    public static TransferObserver uploadNetClient(File file) {
        return upload(getAppLogFolder(), file.getName(), file);
    }



    public static String getAppLogFolder() {
        return BUCKET_NAME + "/" + APP_LOG_PATH +"/"+WholeTimeUtils.millis2String(System.currentTimeMillis() / 1000, "yyyyMMdd");
    }



    public static TransferObserver upload(String path, String fileName, File file) {
        return getTransferUtility().upload(path, fileName, file, CannedAccessControlList.PublicRead);
    }



    public static TransferObserver restart(int id) {
        return getTransferUtility().resume(id);
    }

    public static boolean cancel(int id) {
        return getTransferUtility().cancel(id);
    }

    public static TransferObserver download(String path, String fileName, File file, TransferListener listener) {
        return getTransferUtility().download(path, fileName, file, listener);
    }

    public static TransferUtility getTransferUtility() {
        if (sTransferUtility == null) {
            sTransferUtility = TransferUtility.builder()
                    .context(BaseApplication.sInstance)
                    .s3Client(getS3ClientOld())
                    .awsConfiguration(new AWSConfiguration(BaseApplication.sInstance))
                    .build();
        }
        return sTransferUtility;
    }

    public static AmazonS3Client getS3ClientOld() {
        if (sS3ClientOld == null) {
            ClientConfiguration configuration = new ClientConfiguration();
            configuration.setConnectionTimeout(60 * 1000);
            configuration.setSocketTimeout(60 * 1000);
            sS3ClientOld = new AmazonS3Client(getCredProvider(), configuration);
            try {
                String regionString = new AWSConfiguration(BaseApplication.sInstance).optJsonObject("oZgHVQ3tiFQpLIbTrdiHcBqNfQmyWXc5gDI7RxvZ").getString("backups3d");
                sS3ClientOld.setRegion(Region.getRegion(regionString));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return sS3ClientOld;
    }

    private static AWSCredentialsProvider getCredProvider() {
        if (sMobileClient == null) {
            final CountDownLatch latch = new CountDownLatch(1);
            AWSMobileClient.getInstance().initialize(BaseApplication.sInstance, new Callback<UserStateDetails>() {
                @Override
                public void onResult(UserStateDetails result) {
                    latch.countDown();
                }

                @Override
                public void onError(Exception e) {
                    latch.countDown();
                }
            });
            try {
                latch.await();
                sMobileClient = AWSMobileClient.getInstance();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return sMobileClient;
    }

}
